package com.bmft.jingdong.controller;

import com.bmft.jingdong.constant.IndexConst;
import com.bmft.jingdong.pojo.GoodContent;
import com.bmft.jingdong.util.jsoup.CrawlerUtil;
import com.bmft.jingdong.util.respose.Result;
import com.bmft.jingdong.service.IndexService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author Jarvan
 * @version 1.0
 * @create 2020/11/10 20:23
 * @CrossOrigin 允许跨域请求.
 */
@Log4j2
@RestController
@CrossOrigin("http://localhost:8080")
public class IndexController {
    @Autowired
    @Qualifier("indexService")
    IndexService indexService;
    /**
     * Restful 风格
     * @param key 搜索关键字
     * @return 数据
     */
    @PostMapping(value = {"/list/{key}/{page}","/list/{key}"})
    public Result<List<Map<String, Object>>> getContentList(@PathVariable String key,
                                                            @PathVariable @Nullable String page) throws Exception {
        int from = 0;
        int size = IndexConst.PAGE_SIZE;
        if (page == null){
            page = "0";
        }
        from = Integer.parseInt(page) * size;
        List<Map<String, Object>> goodsContent = indexService.getContentList(key,from,size);
        log.info("返回长度="+goodsContent.size());
        return Result.success(goodsContent);
    }
}
